﻿package com.sz.common.servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sz.message.bmo.IMessageBMO;



public class SendSMS extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try {			
			String telno = request.getParameter("telno");
			String sms = request.getParameter("sms");
			
			String type = request.getParameter("type");
			String ticket_id = request.getParameter("ticket_id"); //订单相关的短信会传此参数
			String business_id = request.getParameter("business_id");  //商家相关的短信会传此参数
			String re_url = request.getParameter("re_url");
			
			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			IMessageBMO msg = (IMessageBMO)context.getBean("MessageBMO");
			msg.sendMessage(telno, sms);
			
			String tip = URLEncoder.encode("短信已发送", "UTF-8");
			if(re_url != null && re_url.length() > 0 ) {
				re_url += "?telno=" + telno;
				re_url += "&type=" + type;
				re_url += "&ticket_id=" + ticket_id;
				re_url += "&business_id=" + business_id;
				re_url += "&tip=" + tip;	
				response.sendRedirect(re_url);
			}

		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}
	}
}
